package com.situ.springboot.controller;

import com.situ.springboot.pojo.entity.Custom;
import com.situ.springboot.pojo.entity.Service;
import com.situ.springboot.pojo.vo.RecordVO;
import com.situ.springboot.service.CustomService;
import com.situ.springboot.service.RecordService;
import com.situ.springboot.service.ServiceService;
import com.situ.springboot.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private ServiceService serviceService;

    @Autowired
    private CustomService customService;

    @RequestMapping("/selectByPage")
    public String selectByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "10") Integer pageSize, Model model) {
        System.out.println("recordController.selectByPage");
        PageInfo pageInfo = recordService.selectByPage(pageNo, pageSize);
        System.out.println("pageInfo:" + pageInfo);
        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        //转发到service_list界面展示
        return "/record/list";
    }



    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id, Model model) {
        RecordVO recordVO = recordService.selectById(id);
        model.addAttribute("recordVO", recordVO);
        return "/record/update";
    }

    @RequestMapping("/update")
    public String update(RecordVO recordVO) {
        recordService.update(recordVO);
        return "redirect:/record/selectByPage";
    }



    // 获取 当前登录用户 的 消费记录 (用于选课)
    @RequestMapping("/selectByLoginCustom")
    public String selectAll(@RequestParam(defaultValue = "1") Integer pageNo,
                            @RequestParam(defaultValue = "10") Integer pageSize,Model model, HttpSession session)
    {
        Custom custom = (Custom) session.getAttribute("custom");
        PageInfo pageInfo = serviceService.selectByPage(pageNo, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        List<RecordVO> recordvos = recordService.selectByCustomId(custom.getId());
        model.addAttribute("recordvos",recordvos);
        return "/service/LoginCustom-list";
    }


    //////////////////////////////////////////////////////////////////////
    @RequestMapping("/selectIfChoose") //判断服务是否被选择 以及 钱够不够选的 (1 2 3)
    @ResponseBody
    public Integer selectIfChoose(@RequestParam("id")Integer serviceId, HttpSession session){
        Custom custom = (Custom) session.getAttribute("custom");

        Integer customId = custom.getId();
        Integer res = recordService.selectByCustomIdAndServiceId(customId, serviceId);//0没选 1选了

        System.out.println("该课程的选择状态" + res);

        if (res==1) return res;
        else { //0

            //2可以选择  3钱不够

            //获取客户余额
            Custom custom1 = customService.selectById(customId);
            Double customMoney = custom1.getMoney();

            //获取服务价格
            Service service = serviceService.selectById(serviceId);
            Double serviceMoney = service.getMoney();

            System.out.println(customMoney+"  "+serviceMoney);

            if(customMoney < serviceMoney) return 3;
            return 2;
        }
    }


    @RequestMapping("/selectByCustomId")
    public String selectByCustomId(Model model,HttpSession session){
        Custom custom =(Custom) session.getAttribute("custom");
        List<RecordVO> recordVO = recordService.selectByCustomId(custom.getId());
        model.addAttribute("recordvo",recordVO);
        return "/record/custom_record_list";
    }
}
